package misc;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Plotter {
	
	public Plotter(){}
	
	public void plotData(String title, String fileName,
			ArrayList<Double> max,ArrayList<Double> avg,ArrayList<Double> min, ArrayList<Double> standardDev, boolean[] plot) {

        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Maximun");
        XYSeries series2 = new XYSeries("Average");
        XYSeries series3 = new XYSeries("Minimum");
        XYSeries series4 = new XYSeries("Standard derivation");
        
        
        for(int i = 0; i < max.size(); i++){
        	series1.add(i,max.get(i));
        	series2.add(i,avg.get(i));
        	series3.add(i,min.get(i));
        	series4.add(i,standardDev.get(i));
        }

        if(plot[0])dataset.addSeries(series1);
        if(plot[1])dataset.addSeries(series2);
        if(plot[2])dataset.addSeries(series3);
        if(plot[3])dataset.addSeries(series4);
        
        JFreeChart chart = ChartFactory.createXYLineChart(
				title,
				"Generation",
				"Fitness",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
		try {
			ChartUtilities.saveChartAsJPEG(new File("charts/"+fileName+".jpg"), chart, 450, 300);
		} catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
			e.printStackTrace();
		}
                
    }
	
	public void plotEntropy(String title, String fileName, ArrayList<Double> Entropy) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Average entropy");
        
        for(int i = 0; i < Entropy.size(); i++){
        	series1.add(i,Entropy.get(i));
        }

        dataset.addSeries(series1);
        
        JFreeChart chart = ChartFactory.createXYLineChart(
				title,
				"Generation",
				"Entropy",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
		try {
			ChartUtilities.saveChartAsJPEG(new File("charts/"+fileName+".jpg"), chart, 450, 300);
		} catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
			e.printStackTrace();
		}
                
    }
	
	public void plotSpikeTrain(String title, String fileName, double[] train) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries series1 = new XYSeries("Spike Train");
        
        for(int i = 0; i < train.length; i++){
        	series1.add(i,train [i]);
        }

        dataset.addSeries(series1);
        
        JFreeChart chart = ChartFactory.createXYLineChart(
				title,
				"Time",
				"Activation",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				false,
				false);
		try {
			ChartUtilities.saveChartAsJPEG(new File("charts/"+fileName+".jpg"), chart, 450, 300);
		} catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
			e.printStackTrace();
		}
                
    }
}